package cn.juque.jdfsystem.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.juque.common.base.BaseRequestVO;
import cn.juque.common.base.BaseResponseVO;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.constants.StringConstants;
import cn.juque.jdfsystem.cache.TpsCache;
import cn.juque.jdfsystem.controller.BaseController;
import cn.juque.jdfsystem.entity.FileInfo;
import cn.juque.jdfsystem.helper.FileInfoHelper;
import cn.juque.jdfsystem.service.IFileInfoService;
import cn.juque.jdfsystem.vo.api.FileInfoDetailVO;
import cn.juque.jdfsystem.vo.api.FileTrustParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>FileInfoApiController</li>
 * </ul>
 * @date 2023-01-12 19:42:59
 **/
@Slf4j
@Controller
@RequestMapping("/jdf/api/file")
public class FileInfoApiController extends BaseController {

    @Resource
    private IFileInfoService fileInfoService;

    @Resource
    private FileInfoHelper fileInfoHelper;

    @Resource
    private TpsCache tpsCache;

    /**
     * 上传
     *
     * @param file       文件
     * @return 文件id
     * @throws IOException IOException
     */
    @PostMapping(value = "/upload")
    @ResponseBody
    public BaseResponseVO<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (ObjectUtil.isNull(file)) {
            return new BaseResponseVO<>(MessageEnum.PARAM_NULL, null);
        }
        this.tpsCache.incrementUpload();
        String platformId = super.getHeaderValue(StringConstants.PLATFORM_ID);
        String fileId = this.fileInfoHelper.saveFileInfo(this.fileInfoService.toFileInfo(file, platformId));
        return new BaseResponseVO<>(MessageEnum.OK, fileId);
    }

    /**
     * 同步变更状态
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/trustFile")
    @ResponseBody
    public BaseResponseVO<Boolean> trustFile(@RequestBody BaseRequestVO<FileTrustParamVO> request) {
        this.validatorUtil.checkNotNull(request);
        this.fileInfoService.trustFile(request.getParam().getValidFileIdList(), request.getParam().getInValidFileIdList());
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 获取文件的详情
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/listFileInfo")
    @ResponseBody
    public BaseResponseVO<List<FileInfoDetailVO>> listFileInfo(@RequestBody BaseRequestVO<List<String>> request) {
        this.validatorUtil.checkNotNull(request);
        List<FileInfo> list = this.fileInfoService.detailFileByIds(request.getParam());
        List<FileInfoDetailVO> voList = BeanUtil.copyToList(list, FileInfoDetailVO.class);
        return new BaseResponseVO<>(MessageEnum.OK, voList);
    }
}
